package com.zg.message.channel.support;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
 * DESCRIPTION
 *
 * @author zhanfeng.liang
 * @since 2025-07-24
 */

public class Demo {
    private final static String appId = "jiaoyi";
    private final static String sm4Key = "A26339A406FE50CA9C40EA0E2896F6ED";
    private final static String customerPrivateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgrn2/GNFQP6bE/lcWExzQhZcHhM/006PGeUQ5Sl4ZlHugCgYIKoEcz1UBgi2hRANCAARPVExjCExjVjsDyCqRybKPCCIvq8+skwee8GeRvdXgIsC0q7LQlQQ4XJuN0N1uBRblAD2tSpIqYDZ1xGBH07IS";
    private final static String sysPublicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE66H9p8wnQagnQcwkrlZ3xfoFJzjTSqJFIzgCMR0ShBxWeId9/UpQWpa2p4vUGxwBK+ZvBGJxScN1dOkJtjk53Q==";
    public static void main(String[] args) throws Exception {
        //原始接口请求入参
        JSONObject requestParams = new JSONObject();
        requestParams.set("mobile", "18211962110");
        requestParams.set("content", "【山西焦煤】 您的短信验证码为：SM01");
        //排序后的请求入参
        TreeMap<String, Object> sortMap = SysEncryptionUtil.sortField(requestParams);
        //生成签名
        String sign = SysEncryptionUtil.sign(customerPrivateKey, appId, JSONUtil.toJsonStr(sortMap));
        //入参中加入签名
        sortMap.put("sign", sign);
        //加密数据
        String cipherText = SysEncryptionUtil.encryptEcb(sm4Key, JSONUtil.toJsonStr(sortMap));
        //调用统一接口
        HttpRequest post = HttpUtil.createPost("http://127.0.0.1:8686/unify-api/sms/sendSms");
        Map<String, String> headers = new HashMap<>();
        headers.put("appId", appId);
        post.addHeaders(headers);
        post.body(cipherText);
        //获取返回结果
        System.out.println("=====================================请求数据=====================================");
        System.out.println(cipherText);
        HttpResponse response = post.executeAsync();
        String responseStr = response.body();
        System.out.println("=====================================响应数据=====================================");
        System.out.println(responseStr);
        System.out.println("=====================================业务处理=====================================");
        JSONObject resultJson = JSONUtil.parseObj(responseStr);
        String decryptStr = SysEncryptionUtil.decryptEcb(sm4Key, resultJson.getStr("data"));
        System.out.println(decryptStr);
        if (JSONUtil.isJson(decryptStr)) {
            JSONObject decryptJson = JSONUtil.parseObj(decryptStr);
            String returnSign = decryptJson.getStr("sign");
            decryptJson.remove("sign");
            TreeMap<String, Object> returnSortMap = SysEncryptionUtil.sortField(decryptJson);
            //验签
            boolean verifySign = SysEncryptionUtil.verifySign(sysPublicKey, appId, returnSign, JSONUtil.toJsonStr(returnSortMap));
            if (verifySign) {
                //验签成功
                System.out.println("验签成功");
                //处理业务逻辑
            } else {
                //验签失败
                System.out.println("验签失败");
            }
        }
    }
}
